module WWW
  class Tkd
    class Gold
      class Utils
        class Misc
          #解析订单,并返回list信息,因为这里可能存在子订单的情况
          def self.parse_trade trade
            orders = Array.new
            if trade['orders']!=nil
              trade_orders = trade['orders']
              trade_orders.each do|item|#整理所有子订单
                if item['iid']!=nil||item['pic_path']!=nil||item['num']!=nil||item['price']!=nil#将非法数据清理
                  order = WWW::Tkd::TbOrder.new
                  order.tid=trade['tid'][0].strip() unless trade['tid']==nil
                  order.status=trade['status'][0].strip() unless trade['status']==nil
                  order.seller_rate=trade['seller_rate'][0].strip() unless trade['seller_rate']==nil
                  order.buyer_rate=trade['buyer_rate'][0].strip() unless trade['buyer_rate']==nil
                  order.iid = item['iid'][0].strip() unless item['iid']==nil
                  order.num=item['num'][0].strip() unless item['num']==nil
                  order.title=item['title'][0].strip() unless item['title']==nil
                  order.price=item['price'][0].strip() unless item['price']==nil
                  order.pic_path=item['pic_path'][0].strip() unless item['pic_path']==nil
                  order.seller_nick = item['seller_nick'][0].strip() unless item['seller_nick']==nil
                  order.buyer_nick = item['buyer_nick'][0].strip() unless item['buyer_nick']==nil
                  order.order_created_at=item['created'][0].strip() unless item['created']==nil
                  order.is_sub =1
                  orders.push order
                else
                  RAILS_DEFAULT_LOGGER.info 'invalid trade:'+trade.to_s
                end
              end
            else
              if trade['iid']!=nil||trade['pic_path']!=nil||trade['num']!=nil||trade['price']!=nil#将非法数据清理
                order = WWW::Tkd::TbOrder.new
                order.seller_nick = trade['seller_nick'][0].strip() unless trade['seller_nick']==nil
                order.buyer_nick = trade['buyer_nick'][0].strip() unless trade['buyer_nick']==nil
                order.iid = trade['iid'][0].strip() unless trade['iid']==nil
                order.title=trade['title'][0].strip() unless trade['title']==nil
                order.price=trade['price'][0].strip() unless trade['price']==nil
                order.pic_path=trade['pic_path'][0].strip() unless trade['pic_path']==nil
                order.num=trade['num'][0].strip() unless trade['num']==nil
                order.tid=trade['tid'][0].strip() unless trade['tid']==nil
                order.order_created_at=trade['created'][0].strip() unless trade['created']==nil
                order.status=trade['status'][0].strip() unless trade['status']==nil
                order.seller_rate=trade['seller_rate'][0].strip() unless trade['seller_rate']==nil
                order.buyer_rate=trade['buyer_rate'][0].strip() unless trade['buyer_rate']==nil
                order.is_sub =0
                orders.push order
              else
                RAILS_DEFAULT_LOGGER.info 'invalid trade:'+trade.to_s
              end
            end
            return orders
          end
        end
      end
    end
  end
end
